package com.qf.fmw.controller;



import com.qf.fmw.entity.vo.RespResult;
import com.qf.fmw.service.GoodsTypeService;
import com.qf.fmw.service.UserService;
import com.qf.fmw.service.impl.GoodsTypeServiceImpl;
import com.qf.fmw.service.impl.UserServiceImpl;
import com.qf.fmw.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.Date;

/*
    管理员
*/

@WebServlet("/AdminController")
//配置上传文件
@MultipartConfig
public class AdminController extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    protected GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();

    //登录
    protected RespResult login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return userService.login(request.getSession(), username, password);
    }

    // 注册
    protected RespResult register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        return userService.register(username, password, email, sex);
    }

    //注销
    protected RespResult logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return userService.logout(request.getSession());
    }

    // 获取所有用户
    protected RespResult getAllUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return userService.showAllUsers();
    }

    //展示用户个人信息
    protected RespResult getMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        return userService.showUserMessage(userId);
    }

    //修改用户个人信息
    protected RespResult updateMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        String userName = request.getParameter("userName");
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        return userService.updateUserMessage(userId, userName, email, sex);
    }

    //修改用户密码
    protected RespResult updatePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        String newPassword = request.getParameter("newPassword");
        return userService.updateUserPassword(userId, newPassword);
    }

    // 根据性别搜索用户
    protected RespResult getUserBySex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gender = request.getParameter("sex");
        return userService.showUserBySex(gender);
    }

    // 根据名字搜索用户
    protected RespResult getUserByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        return userService.showUserByName(name);
    }

    // 删除用户
    protected RespResult deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        return userService.deleteUser(userId);
    }

    // 显示无效用户
    protected RespResult showInvalidUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return userService.showInvalidUsers();
    }

    //根据姓名和性别查询 searchInvalidUserByNameAndGender
    protected RespResult searchInvalidUserByNameAndSex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        return userService.showUserByNameAndSex(name, gender);
    }

    // 根据性别搜索无效用户
    protected RespResult searchInvalidUserBySex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gender = request.getParameter("gender");
        return userService.searchInvalidUserBySex(gender);
    }

    // 根据名字搜索无效用户
    protected RespResult searchInvalidUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String query = request.getParameter("query");
        return userService.searchInvalidUser(query);
    }

    // 删除无效用户
    protected RespResult deleteInvalidUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        return userService.deleteInvalidUser(userId);
    }

    // 添加商品
    protected RespResult addGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
//        String typeId = request.getParameter("typeid");
        Date pubdate = new Date();
        String price = request.getParameter("price");
        String star = request.getParameter("star");
        String intro = request.getParameter("intro");
        Part picturePart = request.getPart("picture");


//         处理图片文件
        String fileName = Paths.get(picturePart.getSubmittedFileName()).getFileName().toString();
        String uploadPath = getServletContext().getRealPath("") + File.separator + "uploads";
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        picturePart.write(uploadPath + File.separator + fileName);

        // 调用服务层添加商品
//       String fileName="";
        return goodsTypeService.addGoods(name, pubdate, price, star, intro, fileName);
    }

    // 获取商品信息
    private String getPartText(Part part) throws IOException {
        StringBuilder value = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                value.append(line);
            }
        }
        return value.toString();
    }
}



